package com.polaris.live.dialog

import android.content.Context
import com.bumptech.glide.Glide
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.dp
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.DialogNormalNoticeBinding
import com.polaris.live.im.sfs.bean.live.payload.NoticeRouterPayloadBean
import com.polaris.live.im.sfs.bean.pusher.PopupWindowMessage
import com.polaris.live.utils.image.loadImage
import com.polaris.live.utils.route.RouterUtils
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 通用的通知弹窗
 *
 * @author Created by lucas on 2023/11/3 11:51
 * @since 1.0.0
 */
class NormalNoticeDialog(
    context: Context,
    private val message: PopupWindowMessage,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_normal_notice
    }

    private lateinit var mBinding: DialogNormalNoticeBinding

    private var delayCloseJob: Job? = null

    override fun onCreate() {
        super.onCreate()

        val payloadBean = JsonUtils.fromJson<NoticeRouterPayloadBean>(message.payload)
        if (payloadBean == null) {
            dismiss()
            return
        }

        mBinding = DialogNormalNoticeBinding.bind(popupImplView)

        mBinding.apply {
            if (message.banner != null) {
                ivBanner.visibility = VISIBLE

                Glide.with(ivBanner)
                    .loadImage(message.banner)
                    .into(ivBanner)
            } else {
                ivBanner.visibility = GONE
            }

            if (message.title != null) {
                tvTitle.visibility = VISIBLE
                tvTitle.text = message.title
            } else {
                tvTitle.visibility = GONE

                ViewUtils.setMargin(tvContent, topMargin = 40.dp, bottomMargin = 40.dp)
            }

            RouterUtils.handleRouterText(
                tvContent,
                message.content,
                payloadBean.highlight ?: emptyList(),
                payloadBean.router ?: emptyMap()
            )

            if (message.confirm != null) {
                tvConfirm.visibility = VISIBLE
                tvConfirm.text = message.confirm
            } else {
                tvConfirm.visibility = GONE
            }
            tvConfirm.setOnClickListener {
                payloadBean.router?.get("confirm")?.let { router ->
                    RouterUtils.routerGoto(router)
                    dismiss()
                } ?: run {
                    dismiss()
                }
            }

            if (message.cancel != null) {
                tvCancel.visibility = VISIBLE
                tvCancel.text = message.cancel
            } else {
                tvCancel.visibility = GONE
            }
            tvCancel.setOnClickListener {
                payloadBean.router?.get("cancel")?.let { router ->
                    RouterUtils.routerGoto(router)
                } ?: run {
                    dismiss()
                }
            }

            if (message.close) {
                icClose.visibility = VISIBLE
            } else {
                icClose.visibility = GONE
            }
            icClose.setOnClickListener {
                dismiss()
            }
        }

        if (message.expiredDuration != null) {
            delayCloseJob = CoroutineScopeManager.mainScope.launch {
                try {
                    delay(message.expiredDuration)

                    dismiss()
                } finally {
                    if (delayCloseJob == coroutineContext[Job]) {
                        delayCloseJob = null
                    }
                }
            }
        }
    }

    override fun onDismiss() {
        delayCloseJob?.cancel()

        super.onDismiss()
    }
}